Ext.define('wmf.controller.role.role', {
    extend: 'Ext.app.Controller',
    stores: ['role.role',
             'role.tree',
             'role.roleGrid'],
    models: ['role.role'],
    views: ['role.main', 
            'role.List', 
            'role.Edit',
            'role.add',
            'role.east',
            'role.modify',
            'role.attribute',
            'role.tree',
            'role.grid'
            ],
    refs: [{
        ref: 'roleList',
        selector: 'roleList'
    },{
        ref: 'roleAdd',
        selector: 'roleAdd'
	},{
        ref: 'roleEast',
        selector: 'roleEast'
	},{
        ref: 'roleTree',
        selector: 'roleTree'
	},{
        ref: 'roleMain',
        selector: 'roleMain'
    },{
        ref: 'roleModify',
        selector: 'roleModify'
    },{
    	ref: 'roleGrid',
    	selector: 'roleGrid'
    },{
    	ref: 'roleAttribute',
    	selector: 'roleAttribute'
    }],
    
    init: function () {
        this.control({
        	'roleList' : {
			    beforerender : this.onBeforeRender
			},
			'roleTree' : {
				itemclick: this.menuClick
			},
			'roleGrid button#save':{
				click: this.onSaveGridButtonClick
			},
			'roleEast tool#minimize': {
				click: this.onMinToolClick
			},
        	'roleAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'roleAdd button#cancel': {
				click: this.onMinToolClick
			},
			'roleList button#add': {
                click: this.onAddButtonClick
            },
			'roleList button#modify': {
                click: this.onModifyButtonClick
            },
            'roleList button#delete': {
                click: this.onRemoveButtonClick
            },
            'roleList button#attribute': {
                click: this.onAttributeButtonClick
            },
			'roleModify button#cancel': {
				click: this.onMinToolClick
			},
			'roleModify button#save': {
				click: this.onModifySaveButtonClick
			}
        });
        wmf.role = {};
        wmf.roleGrid = {};
        wmf.roleGrid.params = {};
    },

    menuClick: function(tree, node, item, index, e, eOpts){
    	var me = this;
    	var grid = me.getRoleGrid();
    	wmf.roleGrid.params.clickResourceId = node.data.resourceId;
    	grid.store.on("load",function(store, records, successful, eOpts){  
 	        for(var i=0;i<records.length;i++) {
 	            var record=records[i];
 	            if(record.get('checked') == 'true') {
 	                grid.getSelectionModel().select(record,true);
 	            }
 	        }
        }); 
		grid.getStore().reload();
    },
    
    onAttributeButtonClick: function(){
    	var select = this.getRoleList().getSelectionModel().getSelection();
		if(select.length != 1){
			Ext.Msg.alert("提示", '请先选择一条数据');
			return false;
		}
    	wmf.roleGrid.params.roleId = select[0].data.id;
        var win = this.getRoleEast();
        win.removeAll();
        win.add(Ext.widget("roleAttribute"));
		win.width = "60%";
        win.hide();
        win.show();
        this.getRoleGrid().getStore().reload();
    },
    
    onSaveGridButtonClick: function(){
    	var me = this;
    	var grid = me.getRoleGrid();
    	var select = grid.getSelectionModel().getSelection();
    	var ids = "";
    	for(var i = 0; i < select.length; i++){
    		ids += wmf.roleGrid.params.clickResourceId + ':' + select[i].data.id;
    		if(i < select.length - 1)
    			ids += ",";
    	}
		Ext.Ajax.request({
			url:'roleAuth/saveAuth.do',
			params: {
				auth: ids,
				roleId: wmf.roleGrid.params.roleId ,
				clickResourceId: wmf.roleGrid.params.clickResourceId
			},
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg?responds.msg:"操作失败");
					return false;
				}
				me.getRoleGrid().getStore().reload();
			},
			failure : function(respond, action) {
	            Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onRemoveButtonClick: function(){
    	var me = this;
    	var select = this.getRoleList().getSelectionModel().getSelection();
    	if(!select[0]){
    		Ext.Msg.alert('提示', "请选择删除数据");
    		return false;
    	}
    	var ids = "";
    	for(var i = 0; i < select.length; i++){
    		ids += select[i].data.id;
    		if(i < select.length - 1)
    			ids += ",";
    	}
    	Ext.MessageBox.confirm('提示','确定要删除吗？',
    		function(btn){
    			if(btn=='yes'){
    				Ext.Ajax.request({
    					url:'role/destroy.do',
    					params: {
    						RID: 2,
    						ids: ids
    					},
    					success : function(respond, opts) {
    						var responds = Ext.decode(respond.responseText);
    						if(responds.failure){
    							Ext.Msg.alert("提示", responds.msg);
    							return false;
    						}
    						me.getRoleList().getStore().reload();
    					},
    					failure : function(respond, action) {
    			            Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
    					}
    				});
    			}else{
    				return;
    			}
    	},this);
    },
    
    onAddButtonClick: function(){
        var win = this.getRoleEast();
        win.removeAll();
        win.add(Ext.widget("roleAdd"));
        win.width = "40%";
        win.hide();
        win.show();
    },
	
	onModifyButtonClick: function(){
		var select = this.getRoleList().getSelectionModel().getSelection();
		if(select.length != 1){
			Ext.Msg.alert("提示", '请先选择一条数据');
			return false;
		}
        var win = this.getRoleEast();
        win.removeAll();
        win.add(Ext.widget("roleModify"));
		var panel = this.getRoleModify();
		win.width = "40%";
        win.hide();
        win.show();
		panel.loadRecord(select[0]);
    },
	
	onAddSaveButtonClick: function(){
    	var me = this;
    	var win = me.getRoleEast();
    	var former = me.getRoleAdd();
    	var value = former.getValues();
    	value.status = 1;
    	Ext.Ajax.request({
			url:'role/save.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getRoleList().getStore().reload();
			},
			failure : function(respond, action) {
				var rs = action.result;
	            msgBox.alert($('wmf.common.fail'),rs&&rs.msg?rs.msg:$('wmf.common.errors.exception'));
			}
		});
    },
	
	onModifySaveButtonClick: function(){
    	var me = this;
    	var win = me.getRoleEast();
    	var former = me.getRoleModify();
    	var value = former.getValues();
    	Ext.Ajax.request({
			url:'role/save.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getRoleList().getStore().reload();
			},
			failure : function(respond, action) {
				var rs = action.result;
	            msgBox.alert($('wmf.common.fail'),rs&&rs.msg?rs.msg:$('wmf.common.errors.exception'));
			}
		});
    },
	
	onMinToolClick: function(){
    	var win = this.getRoleEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getRoleMain().itemId, this.getRoleList());
    	component.getStore().load();
    }
});
